<?php
set_time_limit(0);
$root=preg_replace('/cron$/', '', dirname(__FILE__));

require_once($root."inc.config.php");

require_once($root."initialize.php");

if(($db_msg=cron_initialize()) !== true){

	print_r("Connection error");

}else{ 

	$db=$vars["db"];
	
	$dist = array();
	$cdate = date('Y-m-d H:i:s');
	$bonus_date = date('Y-m', mktime(0, 0, 0, date("m")-1, date("d"), date("Y")));
	$month_date = date('t',mktime(0, 0, 0, date("m")-1, date("d"), date("Y")));

	$r=mysql_query("select * from gg_users where status='normal' and activated='y'");
	while($user = mysql_fetch_assoc($r))
	{
		$dist[$user['id']]['w']=0;
		$dist[$user['id']]['pool_share']=$user['pool_share'];
		$total_share += $user['pool_share'];
	}
	
	$total_bv = @mysql_result(mysql_query("select sum(b.bv) as bv from gg_purchase a inner join gg_sales_bv b on b.slid=a.id where date_format(a.cdate, '%Y-%m')='".$bonus_date."' and status='confirmed' limit 1"), 0) or 0;		
	if($total_bv>0){
		$total_pool_bonus = $total_bv*0.03;
		$unit_pool_bonus = $total_pool_bonus/$total_share;
		
		foreach ($dist as $distid=>$value) {
			if($value['pool_share']>0){
				$pool_bonus = $unit_pool_bonus*$value['pool_share'];
				$balance=mysql_result(mysql_query("select ewallet from gg_users where id=".$distid), 0) or 0;
				$balance2=mysql_result(mysql_query("select mwallet from gg_users where id=".$distid), 0) or 0;
						
				$sql = "INSERT INTO gg_member_comm SET uid=".$distid.",from_uid=".$distid.",type='w',amount=".$pool_bonus.",volume_type=".$unit_pool_bonus.",percent=".$value['pool_share'].",bonus_date='".$bonus_date."-".$month_date."',cdate='".$cdate."'";
				mysql_query($sql);
				
				$sql = "INSERT INTO gg_member_comm_sum SET uid=".$distid.",w=".$pool_bonus.",bonus_date='".$bonus_date."-".$month_date."',cdate='".$cdate."'";
				mysql_query($sql);
						
				$balance+=$pool_bonus;
				mysql_query("update gg_users set ewallet=".$balance." where id=".$distid);
						
				$sql = "INSERT INTO gg_member_ewallet_record SET uid=".$distid.",type='credit',amount=".$pool_bonus.",bal=".$balance.",descr='Monthly Bonus ".$bonus_date."',cdate='".$cdate."'";
				mysql_query($sql);
				
				$maintain = $pool_bonus*0.3;
				$balance-=$maintain;
				$balance2+=$maintain;
				mysql_query("update gg_users set ewallet=".$balance.",mwallet=mwallet+".$maintain." where id=".$distid);
				
				$sql = "INSERT INTO gg_member_ewallet_record SET uid=".$distid.",type='dedit',amount=".$maintain.",bal=".$balance.",descr='Auto Maintain ".$bonus_date.". ".$pool_bonus." X 30% = ".$maintain."',cdate='".$cdate."'";
				mysql_query($sql);				
				$sql = "INSERT INTO gg_member_mwallet_record SET uid=".$distid.",type='credit',amount=".$maintain.",bal=".$balance2.",descr='Auto Maintain ".$bonus_date.". ".$ttl_amount." X 30% = ".$maintain."',cdate='".$cdate."'";
				mysql_query($sql);
				$sql = "INSERT INTO gg_member_maintain SET uid=".$distid.",credit=".$maintain.",rate=1,percent=30,amount=".$maintain.",bonus_date='".$bonus_date."-".$month_date."',cdate='".$cdate."'";
				mysql_query($sql);
			}
		}
		
		mysql_query("update gg_member_comm set status='confirmed' where bonus_date='".$bonus_date."-".$month_date."'");
	}
	
	print_r("finish");
}
?>